package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.InverterStationType;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.InverterStationService;
import com.base.vistter.iframe.service.InverterStationTypeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/station/type")
public class InverterStationTypeController {

    @Resource(name = "inverterStationTypeService")
    private InverterStationTypeService inverterStationTypeService;

    @Resource(name = "inverterStationService")
    private InverterStationService inverterStationService;

    @GetMapping
    public Result<Page<InverterStationType>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum) throws IframeException {
        Page<InverterStationType> page = Page.of(pageNo, pageNum);
        return Result.ok(inverterStationTypeService.page(page));
    }

    @GetMapping("/list")
    public Result<List<InverterStationType>> get() throws IframeException {
        LambdaQueryWrapper<InverterStationType> lambdaQueryWrapper = new LambdaQueryWrapper<InverterStationType>();
        lambdaQueryWrapper.orderByAsc(InverterStationType::getSort);
        return Result.ok(inverterStationTypeService.selectList(lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<InverterStationType> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterStationTypeService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody InverterStationType inverterStationType) throws IframeException {
        return Result.ok(inverterStationTypeService.save(inverterStationType));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody InverterStationType inverterStationType) throws IframeException {
        return Result.ok(inverterStationTypeService.update(inverterStationType));
    }

    @DeleteMapping(produces = "application/json;charset=utf-8")
    public Result<Integer> delete(@RequestBody List<String> ids) throws IframeException {
        return Result.ok(inverterStationTypeService.delete(ids));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        Long count = inverterStationService.selectCount(id);
        if (count > 0l) {
            return Result.error("该电站类型下有电站记录，不能删除");
        } else {
            return Result.ok(inverterStationTypeService.delete(id));
        }
    }

}
